﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShuaiManager : MonoBehaviour
{
    public static ShuaiManager Instance;

    [Header("屏幕属性")]
    public float scale; //屏幕宽高比
    public float height_screen; //屏幕高度
    public float width_screen; //屏幕宽度
    public float width; //实际宽度
    public float cellSize; //单个格子边长
    public float paddingLR; //左右边距
    public float paddingBottom; //底边距

    [Header("预设")]
    public GameObject prefab_Pixel;

    public GameObject prefab_Element;
    public GameObject prefab_Grid;
    public GameObject prefab_Cell;
    public List<GameObject> prefab_Effect_list;
    public GameObject prefab_Attack_N;

    [Header("父节点")]
    public GameObject Parent2D;
    public GameObject ParentElement;
    public GameObject ParentGrid;
    public GameObject ParentCell;
    public GameObject ParentEffect;

    [Header("属性")]
    public int row = 15;
    public int col = 10;
    public int add_row = 0;
    public int type_count;
    public int killNumber = 3;

    [Header("列表")]
    public List<List<Cell>> cellList;
    public List<Cell> movingCellList;


    [Header("游戏状态")]
    public GameState gameState; //游戏核心状态

    public Element current_element;


    private void Awake()
    {
        Instance = this;

        scale = (float)Screen.width / (float)Screen.height;
        height_screen = Camera.main.orthographicSize * 2;
        width_screen = Camera.main.orthographicSize * 2 * scale;
        width = width_screen - paddingLR;
        cellSize = width / col;
    }

    void Start()
    {
        GameStart();
    }

    private void Update()
    {
        if (ShuaiManager.Instance.gameState == GameState.Free)
        {

        }
        else if (ShuaiManager.Instance.gameState == GameState.Fall)
        {

        }
        else if (ShuaiManager.Instance.gameState == GameState.Match)
        {

        }
        else if (ShuaiManager.Instance.gameState == GameState.Result)
        {

        }
        else if (ShuaiManager.Instance.gameState == GameState.Battle)
        {

        }
    }

    public void Clear()
    {
        CellFunctions.ClearAll();
    }

    public void GameStart()
    {
        Clear();

        ShuaiManager.Instance.gameState = GameState.Free;


        movingCellList = new List<Cell>();
        cellList = new List<List<Cell>>();
        for (int x = 0; x <=  col - 1; x++)
        {
            cellList.Add(new List<Cell>());
            for (int y = 0; y <=  row - 1 + add_row; y++)
            {
                cellList[x].Add(null);
                Grid grid = CellFunctions.CreateGrid(x, y);
            }
        }

        ShuaiManager.Instance.current_element = CellFunctions.CreateElement(4, 5);
    }

    //一回合消除部分结束
    public void RoundEnd()
    {
        StartNewRound();
    }

    //开始新的回合
    public void StartNewRound()
    {
        //for (int x = 0; x <= GameManager.Instance.col - 1; x++)
        //{
        //    GameManager.Instance.cellList[x][this.row + 1 - 1] = GameManager.Instance.cellList[x][this.row];
        //    Cell cell = GameManager.Instance.cellList[x][this.row];
        //    cell.y -= 1;
        //    cell.RefreshPos();
        //}
        //CellFunctions.CreateTopLine(this.row + 2);

        ShuaiManager.Instance.gameState = GameState.Free;
        ShuaiManager.Instance.current_element = CellFunctions.CreateElement(4, 5);
    }

}